package com.penjin.android.fragment;

import com.penjin.android.R;
import com.penjin.android.utils.BitmapUtils;
import com.penjin.android.widget.AutoScrollViewPager;
import com.viewpagerindicator.CirclePageIndicator;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class GongzuoFragment extends BaseFragment {
    /**
     * 广告条
     */
    AutoScrollViewPager banner;
    List<View> bannerImages = new ArrayList<>();
    CirclePageIndicator indicator;

    /**
     * 工作按钮区
     */
    ImageView gonggao;
    ImageView rizhi;
    ImageView wendang;
    ImageView shengpi;
    ImageView qiandao;
    ImageView youjian;
    ImageView rili;
    ImageView xinzi;

    static int bannerImageIds[] = {R.drawable.pj_ad_banner_1, R.drawable.pj_ad_banner_2, R.drawable.pj_ad_banner_3};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.layout_fragment_gongzuo, null, false);
        banner = (AutoScrollViewPager) view.findViewById(R.id.banner);
        for (int i = 0; i < bannerImageIds.length; i++) {
            View image = LayoutInflater.from(getActivity()).inflate(R.layout.item_banner_image, null);
            ((ImageView) image.findViewById(R.id.banner_image)).setImageBitmap(BitmapUtils.readBitMap(getActivity(), bannerImageIds[i]));
            bannerImages.add(image);
        }
        BannerAdapter adapter = new BannerAdapter();
        banner.setAdapter(adapter);
        banner.setCurrentItem(0);
        banner.setAutoScrollDurationFactor(7.5f);
        banner.setInterval(3000);
        indicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        indicator.setViewPager(banner);

        gonggao = (ImageView) view.findViewById(R.id.gonggao);
        gonggao.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.gonggao_icon));
        rizhi = (ImageView) view.findViewById(R.id.rizhi);
        rizhi.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.rizhi_icon));
        wendang = (ImageView) view.findViewById(R.id.wendang);
        wendang.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.wendang_icon));
        shengpi = (ImageView) view.findViewById(R.id.shengpi);
        shengpi.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.shenpi_icon));
        qiandao = (ImageView) view.findViewById(R.id.qiandao);
        qiandao.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.qiandao_icon));
        youjian = (ImageView) view.findViewById(R.id.youjian);
        youjian.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.youjian_icon));
        rili = (ImageView) view.findViewById(R.id.rili);
        rili.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.rili_icon));
        xinzi = (ImageView) view.findViewById(R.id.xinzi);
        xinzi.setImageBitmap(BitmapUtils.readBitMap(getActivity(), R.drawable.xinzi_icon));

        gonggao.setOnClickListener(this);
        rizhi.setOnClickListener(this);
        wendang.setOnClickListener(this);
        shengpi.setOnClickListener(this);
        qiandao.setOnClickListener(this);
        youjian.setOnClickListener(this);
        rili.setOnClickListener(this);
        xinzi.setOnClickListener(this);
        return view;
    }

    @Override
    public void onResume() {
        banner.startAutoScroll();
        gonggao.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        rizhi.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        wendang.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        shengpi.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        qiandao.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        youjian.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        rili.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        xinzi.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show_right_in));
        super.onResume();
    }

    @Override
    public void onPause() {
        banner.stopAutoScroll();
        super.onPause();
    }

    private class BannerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return bannerImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(bannerImages.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(bannerImages.get(position));
            return bannerImages.get(position);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gonggao:
                v.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.scale_big));
                break;
            default:
                break;
        }
        super.onClick(v);
    }
}
